package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper,Employee> implements EmployeeService{

    @Autowired
    private EmployeeMapper employeeMapper;
    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public IPage<Employee> selectPage(Integer page, Integer pageSize,String employeName) {
        HashMap<String,Integer> map = new HashMap<>();
        map.put("pageNumver",page);
        map.put("pageSizze",pageSize);
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(employeName!=null,Employee::getName,employeName);
        IPage<Employee> page1 = new Page<>(map.get("pageNumver"),map.get("pageSizze"));
         IPage<Employee> page2 = this.page(page1, queryWrapper);
        return page2;
    }

    /**
     * 保存员工
     * @param employee
     * @return
     */
    @Override
    public boolean saveEmployee(HttpServletRequest request, Employee employee) {

        //首先保证账号唯一
        //身份证号唯一
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(employee.getUsername()!=null,Employee::getUsername,employee.getUsername())
                .eq(employee.getIdNumber()!=null,Employee::getIdNumber,employee.getIdNumber());
        Employee one = this.getOne(queryWrapper);
        if (one!=null){
            return false;
        }
        //插入当前修改人的ID
        Long UserID = (Long) request.getSession().getAttribute("employee");
//        employee.setUpdateUser(UserID);
//        employee.setCreateUser(UserID);
        //给一个初始密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //插入修改时间 和创建时间
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setCreateTime(LocalDateTime.now());
        boolean save = this.save(employee);
        if (save==false){
            return false;
        }
        return true;
    }

    /**
     * 编辑员工
     *
     * @param request
     * @param employee
     * @return
     */
    @Override
    public boolean updateEmployee(HttpServletRequest request, Employee employee) {
        boolean b = this.updateById(employee);
        return b;
    }

    @Override
    public Employee selectByID(Long id) {
        Employee employee = this.getById(id);
        return employee;
    }

    @Override
    public ArrayList<Employee> selectAll1() {
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Employee> employees = employeeMapper.selectList(lambdaQueryWrapper);
        return (ArrayList<Employee>) employees;
    }

}
